package com.runen.infinitemovement.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.MyDynamicStateAdapter;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.ActivityAPI;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.entity.DynamicsEntity;
import com.runen.infinitemovement.model.entity.DynamicsListEntity;
import com.runen.infinitemovement.widget.CustomLoadMoreView;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.JsonUtils;
import com.runen.rxnetlibrary.utils.ToastUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyDynamicStateActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.top_btn_left)
    Button topBtnLeft;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.rv_My_dynamic_state)
    RecyclerView rvMyDynamicState;
    @BindView(R.id.srl)
    SwipeRefreshLayout srl;
    private MyDynamicStateAdapter myDynamicStateAdapter;
    private int page = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_dynamic_state;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        topTitle.setText("我的动态");
        srl.setOnRefreshListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        rvMyDynamicState.setLayoutManager(layoutManager);
        myDynamicStateAdapter = new MyDynamicStateAdapter(this, 1);
        myDynamicStateAdapter.getLoadMoreModule().setLoadMoreView(new CustomLoadMoreView());
        rvMyDynamicState.setAdapter(myDynamicStateAdapter);
        initData();
        initLoadMore();
    }

    private void initLoadMore() {
        myDynamicStateAdapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                loadMoreRequested();
            }
        });
        myDynamicStateAdapter.getLoadMoreModule().setAutoLoadMore(true);
        //当自动加载开启，同时数据不满一屏时，是否继续执行自动加载更多(默认为true)
        myDynamicStateAdapter.getLoadMoreModule().setEnableLoadMoreIfNotFullPage(false);
    }


    @OnClick(R.id.top_btn_left)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onRefresh() {
        page = 2;
        initData();
    }

    private void initData() {
        String str = "?limit=10&page=1";
        srl.setRefreshing(true);
        new ActivityAPI().getMyDynamicsList(str, new BaseResultCallback<DynamicsListEntity>() {
            @Override
            public void onResponse(DynamicsListEntity response) {
                srl.setRefreshing(false);
                if (response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        if (response.data.size() >= response.count) {
                            myDynamicStateAdapter.setNewData(response.data);
                            if (myDynamicStateAdapter.getData().size() >= response.count) {
                                myDynamicStateAdapter.getLoadMoreModule().loadMoreEnd(true);
                                myDynamicStateAdapter.getLoadMoreModule().setEnableLoadMore(false);
                            }
                        }
                    }
                } else {
                    ToastUtils.showShort(response.msg);
                }
            }
        });
    }

    public void loadMoreRequested() {
        String str = "?limit=10&page=%s";
        new ActivityAPI().getMyDynamicsList(String.format(str, page), new BaseResultCallback<DynamicsListEntity>() {
            @Override
            public void onResponse(DynamicsListEntity response) {
                if (response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        myDynamicStateAdapter.addData(response.data);
                        if (myDynamicStateAdapter.getData().size() >= response.count) {
                            myDynamicStateAdapter.getLoadMoreModule().loadMoreEnd(true);
                            myDynamicStateAdapter.getLoadMoreModule().setEnableLoadMore(false);
                        } else {
                            page++;
                            myDynamicStateAdapter.getLoadMoreModule().loadMoreComplete();
                            myDynamicStateAdapter.getLoadMoreModule().setEnableLoadMore(true);
                        }
                    }
                }
            }
        });
    }
}
